package com.powernode.controller;


import com.powernode.pojo.FloorAdmin;
import com.powernode.pojo.TowerAdmin;
import com.powernode.service.FloorAdminService;
import com.powernode.service.TowerAdminService;
import com.powernode.utils.Md5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Hanyixuan
 */
@Controller
public class LoginController {

    @Resource
    public FloorAdminService floorAdminService;

    @RequestMapping("/refresh")
    public String refresh(){
        return "admin";
    }


    @RequestMapping("/admin")
    public String login(HttpServletRequest request ,
                        @RequestParam("floorSite") String floorSite,
                        @RequestParam("password") String password,
                        Model model){
        System.out.println(Md5Util.encode(password));

        FloorAdmin floorAdmin = floorAdminService.selectByFloorSite(floorSite);
        if(floorAdmin!=null && Md5Util.encode(password).equals(floorAdmin.getPassword())){
            HttpSession session= request.getSession();
            session.setAttribute("floorAdmin",floorAdmin);
            return "redirect:/refresh";
        }else {
            model.addAttribute("msg","用户名或密码错误！");
            return "redirect:/login";
        }
    }


}